package com.yulongtian.oneProblemEveryDay.month202211.day20221102;

/**
 * https://leetcode.cn/problems/coordinate-with-maximum-network-quality/
 *
 * @author yulongTian
 * @create 2022-11-02 14:16
 */
public class Test01 {

    public static void main(String[] args) {
        int[][] towers = {{30, 34, 31}, {10, 44, 24}, {14, 28, 23}, {50, 38, 1}, {40, 13, 6}, {16, 27, 9}, {2, 22, 23}, {1, 6, 41}, {34, 22, 40}, {40, 10, 11}};
        int radius = 20;
        int[] ints = bestCoordinate(towers, radius);
        System.out.println("[" + ints[0] + "," + ints[1] + "]");
    }

    //    主函数
    public static int[] bestCoordinate(int[][] towers, int radius) {
        if (towers == null) {
            return null;
        }
        int[] res = new int[2];
        int maxPower = 0;
        for (int x = 0; x <= 50; x++) {
            for (int y = 0; y <= 50; y++) {
                int pointPower = getPointPower(x, y, towers, radius);
                if (maxPower < pointPower) {
                    maxPower = pointPower;
                    res[0] = x;
                    res[1] = y;
                }
            }
        }
        return res;
    }

    //计算某个点获得所有塔的信号
    public static int getPointPower(int x1, int y1, int[][] towers, int radius) {
        int res = 0;
        for (int i = 0; i < towers.length; i++) {
            int d = getD(x1, y1, towers[i][0], towers[i][1]);
            if (d > radius * radius) {
                continue;
            }
            res += (int) (towers[i][2] / (1 + Math.sqrt(d)));
        }
        return res;
    }

    //计算两个点的欧氏距离
    public static int getD(int x1, int y1, int x2, int y2) {
        return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
    }

}
